﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KAImage.Algorithms
{
    /// <summary>
    /// 表示色相指示范围(环形)的闭区间(环形上的一段子集)
    /// </summary>
    public struct ColorRange
    {
        /// <summary>
        /// 循环周期长度
        /// </summary>
        public const int CircleLength = 360;

        public static readonly ColorRange Empty = new ColorRange();

        private void CheckValue(int value)
        {
            if (value < 0 || value > CircleLength)
            {
                throw new Exception("赋值无效，已超出范围[0, 360]");
            }
        }

        /// <summary>
        /// 根据指定的维度初始化 ColorRange 结构的新实例。
        /// </summary>
        /// <param name="s0"></param>
        /// <param name="d0"></param>   
        public ColorRange(int s, int e)
        {
            Starting = s;
            Ending = e;         
        }
      
        /// <summary>
        /// 如果此此结构的最小范围和最大范围均为0，此属性将返回 true；否则将返回 false。
        /// </summary>  
        public bool IsEmpty
        {
            get
            {
                if (Starting == 0 && Ending == 0)
                {
                    return true;
                }
                return false;
            }
        }

        /// <summary>
        /// 获取或设置此结构的范围起点值。
        /// </summary>
        public int Starting;

        /// <summary>
        /// 获取或设置此结构的范围终点值。
        /// </summary>
        public int Ending;
 
        /// <summary>
        /// 判断值是否在区间内
        /// </summary>
        /// <param name="value"></param>
        /// <param name="s"></param>
        /// <param name="d"></param>
        /// <returns></returns>
        public bool Contains(int value)
        {
            if (value == Starting || value == Ending)
            {
                return true;
            }
            if (Ending < Starting)
            {
                if (value <= CircleLength && value > Starting)
                {
                    return true;
                }
                if (value >= 0 && value < Ending)
                {
                    return true;
                }
            }
            else
            {
                if (value > Starting && value < Ending)
                {
                    return true;
                }
            }
            return false;
        }
     
        public override string ToString()
        {
            return string.Format("ColorRange(S0:{0}, D0:{1})", Starting, Ending);
        }
    }
}
